from flask import Flask, Blueprint
from gunicorn.app.wsgiapp import WSGIApplication
import os

# 导入蓝图类
from copernicus import copernicus_app
from bilibili import bilibili_app

# 创建主Flask应用
app = Flask(__name__)

password = 'foobared'

app.config['Agent'] = {"ip":"192.168.2.172","port":"10809"}
app.config['video_save_dir'] = r"/data/fengyy/dateset/video"
app.config['audio_save_dir'] = r"/data/fengyy/dateset/audio"
app.config['CacheDir'] = r"/data/fengyy/dateset/cache"
app.config['Cargs'] = {
        "database":"henan_rsanalysis",
        "user":"postgres",
        "password":"postgresql",
        "host":"192.168.1.71", 
        "port":"5432"
    }
app.config['CELERY_BROKER_URL'] = f'redis://:{password}@192.168.2.172:6379/0'
app.config['CELERY_BACKEND_URL'] = f'redis://:{password}@192.168.2.172:6379/1'


# 注册Blueprint到主应用上
app.register_blueprint(copernicus_app, url_prefix='/copernicus')
app.register_blueprint(bilibili_app, url_prefix='/bilibili')


class CustomWSGIApplication(WSGIApplication):
    app_uri = 'App:app'  # 设置 Flask 应用的 URI
    def init(self, parser, opts, args):
        # 设置 Gunicorn 配置参数
        opts.bind = '0.0.0.0:8880'
        opts.workers = 4
        opts.accesslog = '/data/fengyy/logfile/FlaskBlueprint-gunicorn.log'
        opts.errorlog = '/data/fengyy/logfile/FlaskBlueprint-gunicorn.log'
        opts.daemon = True  # 将 Gunicorn 运行在后台守护进程模式

# gunicorn -w 4 -b 192.168.2.172:8880 App:app

if __name__ == '__main__':
    gunicorn_app = CustomWSGIApplication()
    gunicorn_app.run()